<? include block('htmla');

startScript('jquery, jquery.tablednd, innerxhtml, jquery.sbxa'); ?><script type="text/javascript">

jQuery(function($) {
					 
	$("#objects").tableDnD({
	    onDragClass: "dragging",
	    onDrop: function(table, row) {
			$.post("sort-manual/", {order:$(table).tableDnDSerialize()});
	    },
		dragHandle: "dragHandle"
	});
	
	$("#objects tr").live('mouseenter', function() {
          $(this).addClass('showDragHandle');
	    }).live("mouseleave", function() {
          $(this).removeClass('showDragHandle');
	    });
	
	$("#objects tr a.button").live('click', function() {
		var self = $(this), parent = self.parents("tr"), id = parent.attr("id").substr(4), 
			title = self.attr("title").match(/«(.*)»/)[1], attr = parent.find("input.attr");

		if (self.hasClass('visible')) {
//			$.post("hide-" + id + "/");
			self.removeClass('visible').addClass('hidden').attr("title", "<?=t('Show «!name»', 'tpl')?>".replace("!name", title));
			attr.val(attr.val() & !<?=AA_VISIBLE?>);
		} else

		if (self.hasClass('hidden')) {
//			$.post("show-" + id + "/");
			self.removeClass('hidden').addClass('visible').attr("title", "<?=t('Hide «!name»', 'tpl')?>".replace("!name", title));
			attr.val(attr.val() | <?=AA_VISIBLE?>);
		} else

		if (self.hasClass('kill')) {
			if (confirm("<?=t('Are you sure you want to kill «!name»?', 'tpl')?>".replace("!name", title))) {
				$.post("delete-" + id + "/");
				parent.remove();
			}
		}

		return false;
		
	});

<?	// create table items

if (is_array($d = dat('objects'))) { $temp = array(); 

	?>xTemplate.applyArray({template: $("#objects .template"), data: {<?
		foreach($d as $a) { ob_start();
			?><?=$a['id']?>:{<?
			?>itemId:"<?=$a['id']?>",<?
			?>itemAttr:"<?=$a['attr']?>",<?
			?>itemTag:"<?=$a['tag']?>",<?
			?>itemName:"<?=h($a['name'])?>",<?
			?>itemValue:"<?=h($a['value'])?>",<?
			?>itemLang:"<?=h($a['assoc'])?>",<?
			?>itemText:"<?=h($a['text'])?>"}<?
			$temp[] = ob_get_clean();
		}
		?><?=join(',', $temp)?><?
	?>}, callback: function(block, data) {
		
		block.find("a.button").each(function() {
			var self = $(this), parent = self.parents("tr"), id = parent.attr("id").substr(4), 
				name = self.attr("title").match(/«(.*)»/)[1], attr = parent.find("input.attr");
	
			if (self.hasClass('visible') || self.hasClass('hidden')) {
				if (attr.val() & <?=AA_VISIBLE?>)
					self.removeClass('hidden').addClass('visible').attr("title", "<?=t('Hide «!name»', 'tpl')?>".replace("!name", name));
				else
					self.removeClass('visible').addClass('hidden').attr("title", "<?=t('Show «!name»', 'tpl')?>".replace("!name", name));
			}
		});


		block.find("select").each(function() {
			var self = $(this), parent = self.parents("tr"), id = parent.attr("id").substr(4);
		
			if (self.hasClass('lang') && data[id]['itemLang'])
				self.val(data[id]['itemLang']);
			else
			if (self.hasClass('submenu') && ~~data[id]['itemTag'])
				self.val(data[id]['itemTag']);
		});

		return false;
		
	}});<?
	
	?>$("#objects").tableDnDUpdate();<?
	
} else {
	?>$("#objects").hide();<?
} 

/* create table items */ ?>


	$("#add").submit(function() {
		var f = $(this), title = f.find("input[name='title']").val().trim(), table = $("#objects");

		if (!title) { alert("Can't add unnamed menu item");	return false; }
		
		$.post("add-item/", {title:title}, function(data) {
			if (typeof data == "object") {
				var res = xTemplate.applyData(xTemplate.extractTemplate($("#objects .template")), data);
				$("#objects").show().find("tbody").append(res).end().tableDnDUpdate();
			}
		}, "json");
		
		$("input[name='title']").val("");
		$("input[name='text']").val("")
		return false;
	});



});

</script><? endScript();


?><fieldset><legend> <?=dat('page','name')?> </legend><div><form id="objects-form" action="" method="post"><?

	$m = dat('obj'); 

	?><form action="" method="post">
	<table class="adm dnd xwide" id="objects"><thead><tr class="nodrag nodrop">
		<td>Status</td><td>Language</td><td></td>
		<td>Menu item name</td><td>Link</td><td>Hint</td><td>Submenu</td></tr></thead><tbody><?

		?><tr id="obj-[%itemId]" class="template">
			<td>
				<a title="Show «[%itemName]»" class="button hidden" href="#"></a>
				<a title="Kill «[%itemName]»" class="button kill" href="#"></a>
			</td><td>
				<select name="assoc[[%itemId]]" class="lang"><option value=" "><?=t('Any', 'tpl')?></option>
					<? foreach($DATA['langs'] as $l) { ?><option value="<?=$l['text']?>"><?=$l['assoc']?></option><? } ?>
				</select>
			</td>
			<td class="dragHandle"><input type="hidden" class="attr" name="attr[[%itemId]]" value="[%itemAttr]" />
				<input type="hidden" name="tag[[%itemId]]" value="[%itemTag]" /></td>
			<td><input type="text" class="xwide" name="name[[%itemId]]" value="[%itemName]" /></td>
			<td><input type="text" class="xwide" name="value[[%itemId]]" value="[%itemValue]" /></td>
			<td><input type="text" class="xwide" name="text[[%itemId]]" value="[%itemText]" /></td>
			<td><select name="tag[[%itemId]]" class="submenu">
					<option value="0">No submenu</option>
					<? if (sizeof($m)) foreach ($m as $mid => $mv) { ?>
						<option value="<?=$mid?>"><?=$mv['name']?></option>
					<? } ?>
				</select>
			</td>
		  </tr>

	</tbody></table>

	<input type="submit" name="save_objects" value="Save" />
	</form>

<h3>Add menu item</h3>
<form action="" method="post" id="add">
	<label>Menu item name
		<input type="text" name="title" /></label>
	<input type="submit" name="create_object" value="Create" />
</form>

</fieldset>

<? include block('_htmla');